﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Demo.Model.EntityModels;
using Demo.Business.ViewServices.Abstract;

namespace Demo.WebUI.Controllers
{
    public class PersonController : Controller
    {
		IHomeViewService homeViewService =  Demo.Business.NinjectFactory.Get<IHomeViewService>();
		List<Person> dataList;
		
		public PersonController()
		{
			dataList = homeViewService.ServiceContext.PersonService.GetList();
		}
		
		//
		// GET: /Person/List
		
        public ViewResult List()
        {
            return View(dataList);
        }
		
		//
		// GET: /Person/ListGridPartialView
		
        public PartialViewResult ListGridPartialView()
        {
            return PartialView("ListGridPartialView", dataList);
        }

		//
		// GET: /Person/Edit/id		
        [HttpGet]
        public ViewResult Edit(int id)
        {
			Person t = homeViewService.ServiceContext.PersonService.Get( id);
 
            if (t == null)
            {
                t = new Person();
            }
            return View(t);
        }
		
		//
		// POST: /Person/Edit
		
        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(Person t)
        {
            if (!ModelState.IsValid)
                return View("Edit", t);

            if ( t.id == default(int))
            {
                homeViewService.ServiceContext.PersonService.Insert(t);
            }
            else
            {
                homeViewService.ServiceContext.PersonService.Update(t);
                
            }
            return RedirectToAction("List");
        }
        
		//
		// GET: /Person/Delete
		
        public ActionResult Delete(int id)
        {
            homeViewService.ServiceContext.PersonService.Delete( id);
            return RedirectToAction("List");
        }
    }
}


